package net.handler
{
	import fgame.GameState;
	import ui.UIManager;
	import fgame.controller.Controller;
	import fgame.events.MapEvent;
	import fgame.graphic.Graphic;
	import fgame.scene.SceneManager;
	import fgame.unit.Actor3D;
	
	import flare.collisions.CollisionInfo;
	import flare.collisions.RayCollision;
	import flare.core.Pivot3D;
	
	import flash.geom.Vector3D;
	import flash.utils.setTimeout;
	
	import model.ConfigUnitHelper;
	import model.HeroDataAttribute;
	import model.World;
	import model.user.User;
	
	import net.msg.SvrMsg;

	public class RoomHander
	{
		public function RoomHander()
		{
			
		}
		
		/**
		 * 房间列表
		 * msg:Array=[{id:int,name:String,createName:String,info:{name:String,url:String}},...]
		 */
		public function roomList(msg:Object):void
		{
			var list:Array=ToolUtils.toArray(msg.arg);
			World.HallList=list;
			UIManager.inst.hall.showList(list);
		}
		
		/**
		 * 开始加载地图
		 * msg{id:int,name:String,createName:String,info{name:String,url:String}}
		 */
		public function beginLoading(msg:Object):void
		{
			var url:String=User.currRoom["info"].url;
			UIManager.inst.addLoadingToUILayer();
			
			GameState.inst.sceneManager.removeEventListener(MapEvent.MAP_LOADED,maplaoded);
			GameState.inst.sceneManager.addEventListener(MapEvent.MAP_LOADED,maplaoded);
			GameState.inst.sceneManager.loadMap(url,UIManager.inst.loading.onProgress);
			UIManager.inst.hall.visible=false;
			
			Controller.instance.pingEnable=true;
		}
		
		/**
		 * 地图加载完成
		 */
		private function maplaoded(e:MapEvent):void
		{
			var _role:Actor3D=new Actor3D();
			_role.data=User.heroData;//
			_role.name=User.userName;
			_role.setPosition(Math.random()*40,0,Math.random()*40);
			GameState.inst.sceneManager.addActor(_role);
//			Debug.traceMe(_role.data,"地图加载完成！");
			_role.graphic.wear(_role.data.model);
		
//			var scale:Number=_role.data.scale;
//			Graphic(_role.graphic).setChildScale(scale,scale,scale);

			Controller.instance.target=_role;
//			Controller.instance.cameraBinding=true;
			Controller.instance.controlEnable=true;
			
			var graphic:Pivot3D=_role.graphic as Pivot3D;
			var from:Vector3D = graphic.localToGlobal( new Vector3D( 0, Number(_role.data.radius), 0 ) );
			var dir:Vector3D = graphic.getDown();
			
			var info:CollisionInfo;
			var rayCollisions:RayCollision=GameState.inst.sceneManager.rayCollision;
			if(rayCollisions.test(from,dir))
			{
				info = rayCollisions.data[0];
				graphic.setPosition( info.point.x, info.point.y, info.point.z );
				graphic.setNormalOrientation( info.normal, 0.05 );
			}
			
			var pos:Object={"x":graphic.x,"y":graphic.y,"z":graphic.z,"rotateY":graphic.getRotation().y};
			var infos:Object=_role.data;

			var msg:Object={"info":infos,"pos":pos,"delay":MsgHelper.netDelay};
			var heroInfo:SvrMsg=new SvrMsg("Room",20,msg);
			MsgHelper.sendMsgToS(heroInfo);//设置信息
			
			setTimeout(function():void
			{
				var msg:Object=new SvrMsg("Room",3);//进入场景
				MsgHelper.sendMsgToS(msg);
			},100);
		}
		
		/**
		 * 获取其他英雄信息
		 */
		public function getHero(msg:Object):void
		{
			var list:Array=ToolUtils.toArray(msg.arg);
			var sm:SceneManager=GameState.inst.sceneManager;
			for each(var hero:Object in list)
			{
				if(hero.name!=User.userName)//不是自己
				{
					var _role:Actor3D=sm.getActorByName(hero.name);
					if(!_role)
					{
						_role=new Actor3D();
						_role.data=new Object();
					}
					
					_role.name=hero.name;
					updateHe(_role,hero);

					sm.addActor(_role);
					_role.graphic.wear(_role.data.model);
				}
			}
		}
		
		
		private function updateHe(actor:Actor3D,msg:Object):void
		{
		
			delete msg["name"];
			Debug.traceMe(msg,"updateHe");
			for (var k:String in msg)
			{
				if(k==HeroDataAttribute.INFO)
				{
					actor.data=msg[k];
//					var scale:Number=actor.data.scale;
//					Graphic(actor.graphic).setChildScale(scale,scale,scale);
				}
				else if(k==HeroDataAttribute.POSITION)
				{
					var pos:Object=msg[k];
					actor.setPosition(pos.x,pos.y,pos.z);
					Pivot3D(actor.graphic).rotateY(pos.rotateY);
					
				}else if(k==HeroDataAttribute.NET_DELAY)
				{
					actor.netDelay=msg[k] as Number;
				}
			}

		}
		
		
		public function updateHero(msg:Object):void
		{
			var sm:SceneManager=GameState.inst.sceneManager;
			var _role:Actor3D=sm.getActorByName(msg.name);
			if(_role)
			{
				updateHe(_role,msg);
			}
		}
		
		/**
		 * 移除英雄
		 */
		public function remove(msg:Object):void
		{
			var sm:SceneManager=GameState.inst.sceneManager;
			var userName:String=msg.name;
			sm.remove(userName);
		}
		
		/**
		 * 等待列表
		 * msg{id:int,name:String,createName:String,info{name:String,url:String}}
		 */
		public function roomCreated(msg:Object):void
		{
			User.currRoom=msg;
		}
		
		public function roomNotExist(msg:Object):void
		{
			trace(msg);
		}
		
		public function roomLocked(msg:Object):void
		{
			
		}
		
		/**
		 * 加入等待列表
		 * msg{id:int,name:String,createName:String,info{name:String,url:String}}
		 */
		public function waitingRoomJoined(msg:Object):void
		{
			User.currRoom=msg;
		}
	}
}